package com.jd.product.service.impl;

import com.jd.basic.service.IBaiduAiAuditService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.product.domain.Product;
import com.jd.product.domain.ProductDetail;
import com.jd.product.domain.ProductOnlineAuditLog;
import com.jd.product.mapper.ProductDetailMapper;
import com.jd.product.mapper.ProductMapper;
import com.jd.product.mapper.ProductOnlineAuditLogMapper;
import com.jd.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;


    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Override
    @Transactional
    public void save(Product product) {
        //添加服务 - 自增id
        super.save(product);

        //添加服务详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }

    }

    @Override
    @Transactional
    public void remove(Long id) {
        //删除服务信息
        super.remove(id);
        //删除服务详情
        productDetailMapper.removeByProductId(id);
    }


    @Override
    @Transactional
    public void update(Product product) {
        //修改服务信息
        super.update(product);

        //修改服务详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }
    }

    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        //删除服务信息
        super.patchRemove(ids);
        //删除服务详情
        productDetailMapper.patchRemoveByProductId(ids);
    }



    // 上架
    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        // 1.对每一个服务审核，name和resource
        for (Long id : ids){
            Product product = productMapper.loadById(id);

            // 审核文本
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            // 审核图片
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());

            // 公共信息
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);
            //获取登陆信息id
            Long aLong = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.loadByLogininfoId(aLong);
            log.setAuditId(employee.getId());


            if (boo1 && boo2){
                productMapper.onsale(new Date(),id);
                log.setState(1);
                //审核日志
                log.setNote("审核成功");
                productOnlineAuditLogMapper.save(log);

            }else {
                //3.审核失败：记录审核日志
                log.setState(0);
                log.setNote("审核失败，图片和文本非法!!");
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败：图片，文本非法，或图片不存在");
            }
        }


        return new AjaxResult();
    }
    // 下架
    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        // 操作数据库：改下架时间=当前系统时间，0为下架
        productMapper.offsale(new Date(),ids);
    }
}
